/****************************************************************

kernel/paging.c
Paging support for AWOS

Copyright (c) 2006 Andrew Wilcox. All rights reserved.

Redistribution and use in source and binary forms, with or
without modification, are permitted provided that the following
conditions are met:

1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.

3. The end-user documentation included with the redistribution,
if any, must include the following acknowledgment:

"This product includes software developed by Andrew Wilcox for
AWOS. (http://code.google.com/p/awos)."

Alternately, this acknowledgment may appear in the software
itself, if and wherever such third-party acknowledgments normally
appear.

4. The names "AWOS" and "Andrew Wilcox" must not be used to
endorse or promote products derived from this software without
prior written permission. For written permission, please contact
Andrew.R.Wilcox@Gmail.com.

5. Products derived from this software may not be called "AWOS"
 nor may "AWOS" appear in their name, without prior written
 permission of Andrew Wilcox.

THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL ANDREW WILCOX BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.

****************************************************************/

/** \file paging.c
 * Paging support for AWOS.
 */

#include <kernel.h>
#include <paging.h>

/**
 * Pointer to the start of the page directories.
 * PgDirectory points to the start of the page directories in memory.  As of Build 9 this was at 0x1A0000, or approximately 1.5MB into system RAM.
 * \warning The location of the pointer is subject to change in later revisions.
 */
unsigned long *PgDirectory = (unsigned long *)0x1A0000;		//map to appx 1.5MB into RAM
/**
 * Pointer to the start of the page tables.
 * PgTable points to the start of the page tables in memory.  As of Build 9 this was at 0x1A1000, directly after PgDirectory.
 * \warning The location of the pointer is subject to change in later revisions.
 */
unsigned long *PgTable = (unsigned long *)0x1A1000;		//map in right after directory

/*! \fn void KsPaging(int memkb);
 *  \brief Sets up page tables.
 *  \param memkb The amount of installed memory in the system, in kilobytes.
 *  \return TRUE if successful, FALSE if unsuccessful.
 *  The function writes the address of #PgDirectory to CR3.  It reads CR3 back in, and if CR3 is equal to the address of PgDirectory, it returns TRUE.
 */
BOOL KsPaging(int memkb)
{
	unsigned long address = 0;
	int i;
	
	for(i = 0; i < 1024; i++)
	{
		PgTable[i] = address | PGD_PRESENT | PGD_WRITABLE | PGD_GLOBAL;
		address += 4096;
	};
	
	for(; i < ((memkb * 1024) / 4); i++)
	{
		//mark any system RAM left as present
		PgTable[i] = address | PGD_PRESENT | PGD_WRITABLE | PGD_USER | PGD_SWAPIN;
		address += 4096;
	};
	
	address = (unsigned long)PgTable;	//where the page tables start
	
	PgDirectory[0] = address | PGD_PRESENT | PGD_WRITABLE;
	address += 4096;
	
	for(i = 1; i < ((memkb / 1024) / 4); i++)
	{
		PgDirectory[i] = address | PGD_PRESENT | PGD_WRITABLE | PGD_USER;
		address += 4096;
	};
	
	for(; i < 1024; i++)
	{
		PgDirectory[i] = 0;		//NOT present
	};
	
	asm("mov %0, %%cr3" :: "r"(0x1A0000));
	asm("mov %%cr0, %0" : "=r"(i));
	i |= 0x80000000;
	asm("mov %0, %%cr0" :: "r"(i));
	
	asm("mov %%cr3, %0" : "=r"(i));
	if(i == (int)PgDirectory)
		return TRUE;
	else
		return FALSE;
}
